#include "resultsviewer.h"
#include "player.h"
#include <QList>
#include <QPainter>
#include <QPaintEvent>

ResultsViewer::ResultsViewer(QWidget *parent)
	: QWidget(parent) 
{
}
		
void ResultsViewer::setMode(Mode mode, const QList<Player*> &players)
{
	m_mode = mode;
	m_players = players;
}
		
void ResultsViewer::paintEvent(QPaintEvent *event)
{
	if (m_mode == GameResult) {
		QPainter painter(this);
		painter.setRenderHint(QPainter::Antialiasing);
		
		QLinearGradient gradient(rect().topLeft(), rect().bottomRight());
		gradient.setColorAt(0.0, Qt::black);
		gradient.setColorAt(1.0, QColor(64, 64, 64));
		
		painter.setPen(Qt::NoPen);
		painter.setBrush(QBrush(gradient));
		painter.drawRect(rect());
		
		painter.setFont(QFont("Helvetica", 12, QFont::Bold));
		
		QListIterator<Player*> i(m_players);
		int x = 1;
		while (i.hasNext()) {
			QRect box(rect().width() / 3, x * 30, rect().width() / 3, 25);
			gradient.setStart(box.topLeft());
			gradient.setFinalStop(box.bottomRight());
			gradient.setColorAt(0.0, QColor(64, 64, 64));
			gradient.setColorAt(1.0, Qt::darkGray);
			painter.setBrush(QBrush(gradient));
			painter.setPen(Qt::NoPen);
			painter.drawRoundedRect(box, 10, 10);
			
			Player *player = i.next();
			painter.setPen(Qt::white);
			painter.drawText(box, Qt::AlignVCenter | Qt::AlignHCenter, 
				player->getName() + ": $" + QString::number(player->getMoney()));
			x++;
		}
	}
}